
from django.db import models

class User(models.Model):
    username = models.CharField(max_length=20, unique=True, verbose_name="用户")
    password = models.CharField(max_length=255, verbose_name="密码")

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = '用户'
        indexes = [
            models.Index(fields=['username'], name='idx_username'),
        ]

class UserPoints(models.Model):
    """用户积分变更明细表"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    points = models.IntegerField(verbose_name="积分变动值")
    reason = models.CharField(max_length=255, verbose_name="变动原因")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="记录时间")

    class Meta:
        db_table = 'user_points'
        verbose_name = '用户积分明细'
        verbose_name_plural = '用户积分明细'
        indexes = [
            models.Index(fields=['user', 'create_time'], name='idx_user_time'),
        ]

class UserRanking(models.Model):
    """用户排行榜（每日快照）"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="用户")
    points = models.IntegerField(default=0, verbose_name="总积分")
    rank = models.IntegerField(default=0, verbose_name="排名")
    date = models.DateField(verbose_name="统计日期")

    class Meta:
        db_table = 'user_ranking'
        verbose_name = '用户排行榜'
        verbose_name_plural = '用户排行榜'
        indexes = [
            models.Index(fields=['date', 'rank'], name='idx_date_rank'),
            models.Index(fields=['user'], name='idx_user_ranking'),
        ]
